<?php

class Admin_UserController extends Custom_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
	}

    public function indexAction()
    {
		//search section
		$objUser = new User_Model_DbTable_User();
		$page = $this->getRequest()->getParam('page');
		$objUser->setPaginator($this->Config->params->recordsPerPage,$page);
		$this->setSort($objUser);
       // echo $this->searchKey;die;
		$this->view->users = $objUser->fetchUsersWithRole($this->searchKey);
		$this->view->paginator = $objUser->getPaginator();
		$this->view->page = $page;

		if($this->isAjax)
		{
			echo Zend_Json_Encoder::encode($this->view->users);
			exit;
		}

		#set submenu
		$this->_setTab(0);
	}

    public function createAction()
    {
        $form = new Admin_Form_AddUser();
        $this->view->form = $form;

		if($this->getRequest()->isPost())
		{
			$formData = $this->getRequest()->getPost();

			if($form->isValid($formData))
			{
                $objUser = new User_Model_DbTable_User();
				$postedData = $form->getValues();
				$postedData['password'] = Custom_General::encrypt($postedData['password']);
				$newUserId = $objUser->insert($postedData);

				#update or create profile
				if($newUserId)
				{
					$this->addMessage('User added successfully');
					$this->Redirector('index','user','admin');
				}
				else if($form->getError())
					$this->addMessage($objUser->getError(),'error');
				else
					$this->addMessage('User not added successfully');
		   }
		}

		#set submenu
		$this->_setTab(1);
    }

    public function editAction()
    {
		$user_id	= $this->_getParam('id');

		if($user_id)
		{
			$objUser	= new User_Model_DbTable_User();
			$form		= new Admin_Form_AddUser($user_id);
			$user_info	= $objUser->getOne($user_id);

			$form->populate($user_info);

			$this->view->form = $form;

			if($this->getRequest()->isPost())
			{
				$formData = $this->getRequest()->getPost();

				if($form->isValid($formData))
				{
					$postedData = $form->getValues();
					if(empty($postedData['password']))
                        unset($postedData['password']);
                    else
                        $postedData['password'] = Custom_General::encrypt($postedData['password']);
					
					$res = $objUser->updateById($postedData,$user_id);

					if($res)
						$this->addMessage('User information updated successfully');
					else if($objUser->getError())
						$this->addMessage($objUser->getError(),'error');

					if($this->_getParam('page'))
						$params = array('id'=>$id, 'page'=>$this->_getParam('page'));
					else
						$params = array('id'=>$id);

					$this->Redirector('index','user','admin',$params);
			   }
			}
		}
		else
		{
			$this->addMessage('User not found');
		}

		#set submenu
		$this->_setTab(2);
	}

    public function deleteAction()
    {
        $id = $this->_getParam('id');

		if($id)
		{
			$objUser = new User_Model_DbTable_User();
			$res = $objUser->deleteById($id);

			if($res)
				$this->addMessage('User delete successfully');
			else
				$this->addMessage('User not delete successfully','error');

			if($this->_getParam('page'))
				$params = array('page'=>$this->_getParam('page'));
			else
				$params = array();

			$this->Redirector('index','user','admin',$params);
		}
    }

	public function showAction()
	{
		$id = $this->_getParam('id');

		if($id)
		{
			$objUser	= new User_Model_DbTable_User();
			$objBank    = new Service_Model_DbTable_Bank();

            $data		= $objUser->getOne($id);
           	$profile	= $objProfile->getOneByColumn('user_id',$data['id']);
            $bank       = $objBank->getOne($data['bank_id']);

           	$data['Profile'] = $profile;
            $data['Bank'] = $bank;

            
			$this->view->data = $data;
			$this->view->profileImagePath = $this->Config->params->user->imagePath;
		}
		else
		{
			$this->view->data = false;
		}

        #set submenu
		$this->_setTab(3);
	}
	
	private function _setTab($activeIndx)
	{
		$subItems = array(__('Manage User'),__('Create User'),__('Edit User'), __('User Details'));
		$subItemLinks = array('admin/user','admin/user/create','#','#');

		$menues = array('0','1');
		if(!in_array($activeIndx, $menues)) $menues[] = $activeIndx;

		$subMenues = array('items' => $subItems, 'links'=> $subItemLinks,
						   'enableMenues' => $menues, 'activeIndx' => $activeIndx);

		$this->view->subMenues = $subMenues;
	}
}







